package nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * User: Administrator
 * Date: 2021/4/13
 * Time: 11:21
 * Description:
 */
public class NioDiscardClient {
    public static void main(String[] args) throws IOException {
        InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 9088);

        // 1、获取通道
        SocketChannel socketChannel = SocketChannel.open(inetSocketAddress);
        socketChannel.configureBlocking(false);
        // 2、连接前做其他事情
        while(!socketChannel.finishConnect()) {
            System.out.println("连接前做其他事");
        }
        System.out.println("客户端连接成功");
        // 分配指定大小缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        byteBuffer.put("hello nio".getBytes());
        byteBuffer.flip();
        // 发送服务器
        socketChannel.write(byteBuffer);
        socketChannel.shutdownOutput();
        socketChannel.close();
    }
}
